package leetcode.code1673;

import java.util.Stack;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int[] mostCompetitive(int[] nums, int k) {
		int len = nums.length;
		Stack<Integer> stack = new Stack<>();
		for (int i = 0; i < len; i++) {
			int num = nums[i];
			while (!stack.isEmpty() && nums[stack.peek()] > num && stack.size() + len - i > k) {
				stack.pop();
			}
			if (stack.size() < k)
				stack.add(i);
		}
		int[] ans = new int[k];
		int p = k - 1;
		while (!stack.isEmpty()) {
			ans[p--] = nums[stack.pop()];
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(H.str2array3("[2,3,3,4]"), this.mostCompetitive(H.str2array3("[2,4,3,3,5,4,9,6]"), 4));
		H.compare(H.str2array3("[2,6]"), this.mostCompetitive(H.str2array3("[3,5,2,6]"), 2));
		H.compare(H.str2array3("[1,1,9,9,9,9]"), this.mostCompetitive(H.str2array3("[2,4,1,3,5,4,1,9,9,9,9]"), 6));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
